package com.itheima.service.impl;

import com.itheima.dao.RoleDao;
import com.itheima.domain.Role;
import com.itheima.mapper.RoleMapper;
import com.itheima.service.RoleService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service("roleService")
public class RoleServiceImpl implements RoleService{

    private static SqlSession sqlSession;

    static {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            sqlSession = sqlSessionFactory.openSession(true);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> list() {
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
        List<Role> roleList1 = roleMapper.findAll();
        //List<Role> roleList = roleDao.findAll();
        return roleList1;
    }

    @Override
    public void save(Role role) {

        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
        roleMapper.save(role);
        //roleDao.save(role);
    }

}
